package com.woniuxy.java106crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.java106crm.entity.Perms;
import com.woniuxy.java106crm.entity.Role;
import com.woniuxy.java106crm.entity.User;
import com.woniuxy.java106crm.mapper.UserMapper;
import com.woniuxy.java106crm.request.ResPage;
import com.woniuxy.java106crm.service.UserService;
import com.woniuxy.java106crm.vo.LoginVo;
import com.woniuxy.java106crm.vo.SelectUserVo;
import com.woniuxy.java106crm.vo.ServiceInvoiceVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private AuthenticationManager authenticationManager;

    @Resource
    private UserMapper userMapper;
    @Override
    public User login(LoginVo loginVo) {
        //只能让security 去执行登录的流程
        //将用户账号密码信息封装成Authentication
        UsernamePasswordAuthenticationToken upat = new UsernamePasswordAuthenticationToken(loginVo.getUsername(), loginVo.getPassword());
        //调用security的认证流程
        Authentication authentication = authenticationManager.authenticate(upat);
        //
        log.debug("登陆成功");

        //获取用户信息
        return (User) authentication.getPrincipal();
    }

    @Override
    public List<Perms> getMenu(List<Role> roleList) {
        List<Perms> permsList = roleList.get(0).getPermsList();
        List<Perms> father = new ArrayList<>();
        for (Perms perms : permsList){
            if(perms.getParentid().equals(0)){
                father.add(perms);
            }
        }
        for (Perms perms : father){
            List<Perms> children = new ArrayList<>();
            for(Perms perms1 : permsList){
                if(perms1.getParentid().equals(perms.getId())){
                    children.add(perms1);
                }
            }
            perms.setChildren(children);
        }
        return father;
//        System.out.println(father);

    }

    @Override
    public User findById(Integer id) {
        return userMapper.findById(id);
    }
    //查询客户经理
    @Override
    public List<SelectUserVo > selectManager() {
        return userMapper.selectManager();
    }

    //查询全部系统用户
    @Override
    public List<User> queryUsers(User user) {
        return userMapper.queryUsers(user);
    }

    //添加系统用户
    @Override
    public Boolean addUser(User user) {
        return userMapper.addUser(user);
    }

    //删除用户
    @Transactional
    @Override
    public Boolean deleteUser(Integer id) {
        //删除用户关联关系表的用户对应角色
        userMapper.deleteUserRole(id);
            //删除用户
        return userMapper.deleteUser(id);
    }

    //批量删除用户
    @Transactional
    @Override
    public void deleteUsers(String ids) {
       for(String id : ids.split(",")){
           userMapper.deleteUserRole(Integer.parseInt(id));
           userMapper.deleteUser(Integer.parseInt(id));
       }
    }
    //给用户分配权限
    @Transactional
    @Override
    public void assginRole(Integer uid, String rids) {
        //1.删除用户已有权限
        userMapper.deleteUserRole(uid);
        //2.添加新的权限
        for(String rid : rids.split(",")){
            userMapper.addUserRole(uid, Integer.parseInt(rid));
        }
    }
    //编辑用户
    @Override
    public Boolean updateUser(User user) {
        return userMapper.updateUser(user);
    }


}
